package com.example.tests;

import com.example.fw.RandomHelper;

import java.util.List;

import static com.example.fw.RandomHelper.getRnd;

public class AddressData implements Comparable<AddressData> {
    private String firstName;
    private String lastName;
    private String addressString;
    private String homePhone;
    private String cellPhone;
    private String workPhone;
    private String eMail1;
    private String eMail2;
    private String birthDay;
    private String birthMonth;
    private String birthYear;
    private String groupName;
    private String addressString2;
    private String homePhone2;
    private String contactPhone;
    private String contactMail;
    private String fullName;
    private String birthDate;
    private String id;

    private void updateContactPhone() {
        if ((homePhone != null) && (homePhone.length() != 0))
            contactPhone = homePhone.replace(" ", "");
        else if ((cellPhone != null) && (cellPhone.length() != 0))
            contactPhone = cellPhone.replace(" ", "");
        else if ((cellPhone != null) && (cellPhone.length() != 0))
            contactPhone = workPhone.replace(" ", "");
        else contactPhone = workPhone;
    }

    private void updateContactMail(){
        if ((eMail1 != null) && (eMail1.length() != 0))
            contactMail = eMail1.replace("%20", " ");
        else if ((eMail2 != null) && (eMail2.length() != 0))
            contactMail = eMail2.replace("%20", " ");
        else
            contactMail = eMail2;
    }

    private void updateFullName(){
        if (("" + firstName + lastName).length() == 0)
            fullName = "";
        else if ((("" + firstName).length() == 0) || (("" + lastName).length() == 0))
            fullName = firstName + lastName;
        else
            fullName = firstName + " " + lastName;
    }

    private void updateBirthDate(){
        birthDate = ""
                + (("" + birthDay).equals("-") ? "" : birthDay) + "."
                + (("" + birthMonth).equals("-") ? "" : birthMonth) + "."
                + birthYear;
        if (birthDate.equals(".."))
            birthDate = "";
    }

    private boolean isNull(String string){
        return (string == null) || string.equals("null");
    }

    protected void setDirectBirthDate(String birthDate) {
        this.birthDate = birthDate;
    }

    public AddressData(String firstName, String lastName, String addressString, String homePhone, String cellPhone, String workPhone, String eMail1, String eMail2, String birthDay, String birthMonth, String birthYear, String groupName, String addressString2, String homePhone2) {
        setFirstName(firstName);
        setLastName(lastName);
        this.addressString = addressString;
        this.homePhone = homePhone;
        this.cellPhone = cellPhone;
        this.workPhone = workPhone;
        this.eMail1 = eMail1;
        this.eMail2 = eMail2;
        this.birthDay = birthDay;
        this.birthMonth = birthMonth;
        this.birthYear = birthYear;
        this.groupName = groupName;
        this.addressString2 = addressString2;
        this.homePhone2 = homePhone2;
        updateContactPhone();
        updateContactMail();
        updateFullName();
        updateBirthDate();
    }

    public AddressData(){
        this("", "", "", "", "", "", "", "", "", "", "", "", "", "");

        //Setting default combo box values.
        setBirthDay("-");
        setBirthMonth("-");
        setGroupName("[none]");
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getAddressString() {
        return addressString;
    }

    public String getHomePhone() {
        return homePhone;
    }

    public String getCellPhone() {
        return cellPhone;
    }

    public String getWorkPhone() {
        return workPhone;
    }

    public String geteMail1() {
        return eMail1;
    }

    public String geteMail2() {
        return eMail2;
    }

    public String getBirthDay() {
        return birthDay;
    }

    public String getBirthMonth() {
        return birthMonth;
    }

    public String getBirthYear() {
        return birthYear;
    }

    public String getGroupName() {
        return groupName;
    }

    public String getAddressString2() {
        return addressString2;
    }

    public String getHomePhone2() {
        return homePhone2;
    }

    public String getContactPhone() {
        return contactPhone;
    }

    public String getContactMail() {
        return contactMail;
    }

    public String getFullName() {
        return fullName;
    }

    public String getBirthDate() {
        return birthDate;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setFirstName(String firstName) {

        this.firstName = ("null".equals(firstName) ? null : firstName);
        updateFullName();
    }

    public AddressData withFirstName(String firstName) {
        setFirstName(firstName);
        return this;
    }

    public void setLastName(String lastName) {
        this.lastName = ("null".equals(lastName) ? null : lastName);
        updateFullName();
    }

    public AddressData withLastName(String lastName) {
        setLastName(lastName);
        return this;
    }

    public void setAddressString(String addressString) {
        this.addressString = addressString;
    }

    public AddressData withAddressString(String addressString) {
        setAddressString(addressString);
        return this;
    }

    public void setHomePhone(String homePhone) {
        this.homePhone = homePhone;
        updateContactPhone();
    }

    public AddressData withHomePhone(String homePhone) {
        setHomePhone(homePhone);
        return this;
    }

    public void setCellPhone(String cellPhone) {
        this.cellPhone = cellPhone;
        updateContactPhone();
    }

    public AddressData withCellPhone(String cellPhone) {
        setCellPhone(cellPhone);
        return this;
    }

    public void setWorkPhone(String workPhone) {
        this.workPhone = workPhone;
        updateContactPhone();
    }

    public AddressData withWorkPhone(String workPhone) {
        setWorkPhone(workPhone);
        return this;
    }

    public void seteMail1(String eMail1) {
        this.eMail1 = eMail1;
        updateContactMail();
    }

    public AddressData withEMail1(String eMail1) {
        seteMail1(eMail1);
        return this;
    }

    public void seteMail2(String eMail2) {
        this.eMail2 = eMail2;
        updateContactMail();
    }

    public AddressData withEMail2(String eMail2) {
        seteMail2(eMail2);
        return this;
    }

    public void setBirthDay(String birthDay) {
        this.birthDay = birthDay;
        updateBirthDate();
    }

    public AddressData withBirthDay(String birthDay) {
        setBirthDay(birthDay);
        return this;
    }

    public void setBirthMonth(String birthMonth) {
        this.birthMonth = birthMonth;
        updateBirthDate();
    }

    public AddressData withBirthMonth(String birthMonth) {
        setBirthMonth(birthMonth);
        return this;
    }

    public void setBirthYear(String birthYear) {
        this.birthYear = birthYear;
        updateBirthDate();
    }

    public AddressData withBirthYear(String birthYear) {
        setBirthYear(birthYear);
        return this;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public AddressData withGroupName(String groupName) {
        setGroupName(groupName);
        return this;
    }

    public void setAddressString2(String addressString2) {
        this.addressString2 = addressString2;
    }

    public AddressData withAddressString2(String addressString2) {
        setAddressString2(addressString2);
        return this;
    }

    public void setHomePhone2(String homePhone2) {
        this.homePhone2 = homePhone2;
    }

    public AddressData withHomePhone2(String homePhone2) {
        setHomePhone2(homePhone2);
        return this;
    }

    public AddressData storeUnChangedFields(AddressData that){
        AddressData result = new AddressData();

        if (isNull(this.getFirstName()))
            result.setFirstName(that.getFirstName());
        else
            result.setFirstName(this.getFirstName());

        if (isNull(this.getLastName()))
            result.setLastName(that.getLastName());
        else
            result.setLastName(this.getLastName());

        return result;
    }


    public void selectRandomGroup(List<String> groupList){
        if ((groupList != null) && (groupList.size() > 0))
            groupName = groupList.get(getRnd().nextInt(groupList.size()));
    }

    @Override
    public int compareTo(AddressData that) {
        int result;
        if ((result = ("" + this.lastName).toLowerCase().compareTo(("" + that.getLastName()).toLowerCase())) != 0)
            return result;
        else
            return ("" + this.firstName).toLowerCase().compareTo(("" + that.getFirstName()).toLowerCase());
    }

    @Override
    public String toString() {
        return "AddressData{" +
                "lastName='" + lastName + '\'' +
                ", firstName='" + firstName + '\'' +
                ", addressString='" + addressString + '\'' +
                ", contactPhone='" + contactPhone + '\'' +
                ", e-mail='" + contactMail + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        AddressData that = (AddressData) o;
        return ("" + this.lastName).equals("" + that.lastName)
                && ("" + this.firstName).equals("" + that.firstName);

    }

    public boolean completelyEquals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        AddressData that = (AddressData) o;
        return ("" + this.firstName).equals("" + that.firstName)
                && ("" + this.lastName).equals("" + that.lastName)
                && ("" + this.addressString).equals("" + that.addressString)
                && ("" + this.homePhone).equals("" + that.homePhone)
                && ("" + this.cellPhone).equals("" + that.cellPhone)
                && ("" + this.workPhone).equals("" + that.workPhone)
                && ("" + this.eMail1).equals("" + that.eMail1)
                && ("" + this.eMail2).equals("" + that.eMail2)
                && ("" + this.birthDay).equals("" + that.birthDay)
                && ("" + this.birthMonth).equals("" + that.birthMonth)
                && ("" + this.birthYear).equals("" + that.birthYear)
                && ("" + this.addressString2).equals("" + that.addressString2)
                && ("" + this.homePhone2).equals("" + that.homePhone2);
    }

    @Override
    public int hashCode() {
        return 1;
    }

    public void setBirthDate(String birthDate){
        setDirectBirthDate(birthDate);
    }

    //избавляемся от строк "null", которые плодит SortedListOf
    public AddressData withNoNulls(){
        if ((this.lastName == null) || (this.lastName.equals("null"))){
            this.lastName = "";
        }
        if ((this.firstName == null) || (this.firstName.equals("null"))){
            this.firstName = "";
        }
        return this;
    }
}
